import {login, getInfo, logout} from "@/api/user";
import {getToken, setToken, removeToken} from '@/utils/auth'

const user = {
    state: {
        token: getToken(),
        name: '',
        avatar: '',
        userID: 0,
        roles: [],
        permissions: []
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_USERID: (state, id) => {
            state.userID = id
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        },
    },

    actions: {
        // 登录
        Login({commit}, loginForm) {
            const username = loginForm.username.trim()
            const password = loginForm.password
            const IPAddr = loginForm.IPAddr

            return new Promise((resolve, reject) => {
                login(username, password, IPAddr)
                    .then(res => {
                        setToken(res.data)
                        commit('SET_TOKEN', res.data)
                        resolve(res)
                    })
                    .catch(error => {
                        reject(error)
                    })
            })
        },

        // 获取用户信息
        GetInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const user = res.data
                    // const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
                    const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : user.avatar;

                    // if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                    //     commit('SET_ROLES', res.roles)
                    //     commit('SET_PERMISSIONS', res.permissions)
                    // } else {
                    //     commit('SET_ROLES', ['ROLE_DEFAULT'])
                    // }
                    commit('SET_NAME', user.userName)
                    commit('SET_AVATAR', avatar)
                    commit('SET_USERID', user.userID)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({commit, state}) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    // commit('SET_ROLES', [])
                    // commit('SET_PERMISSIONS', [])
                    commit('SET_TOKEN', '')
                    removeToken()
                    sessionStorage.removeItem('session');
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

    }
}

export default user
